import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Test03Client
 */
public class Test03Client {

  public static void main(String[] args) throws Exception {
    // 1.定义Scanner类和要录入的字符串
    Scanner sc = new Scanner(System.in);
    // 2.定义集合存放老师信息
    List<Teacher> list = new ArrayList<Teacher>();
    // 3.逐行录入老师信息，直到遇到quit结束录入
    System.out.println("客户端：请录入老师的信息，格式为：姓名-年龄-工资");
    System.out.println("输入quit结束录入");
    String str;
    while ((str = sc.nextLine()) != null) {
      if ("quit".equals(str)) {
        break;
      }
      // 3.1 拆分录入的字符串
      String[] text = str.split("-");
      String name = text[0];
      // 3.2 创建Teacher对象，并为对应属性赋值
      Teacher teacher = new Teacher();
      teacher.setName(name);
      teacher.setAge(Integer.parseInt(text[1]));
      teacher.setSalary(Double.parseDouble(text[2]));
      // 3.3 将老师对象添加到集合中
      list.add(teacher);
    }
    // 4.搭建TCP客户端
    String ip = "127.0.0.1";
    int port = 8002;
    Socket client = new Socket(ip, port);
    System.out.println("客户端：启动成功" + client);
    // 5.发送集合对象
    // 5.1 获取输出流，并包装成对象流
    ObjectOutputStream oos = new ObjectOutputStream(client.getOutputStream());
    // 5.2 发送集合对象
    oos.writeObject(list);
    // 6.从服务器接收数据
    // 6.1 获取输出流并包装成缓冲流
    BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
    // 6.2 读取信息并在控制台打印
    System.out.println("客户端：收到服务器信息" + br.readLine());
    // 7.关闭资源
    br.close();
    oos.close();
    client.close();
    System.out.println("客户端：已关闭");
  }
}